package com.c2soft.nepalcmx.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;




import com.c2soft.nepalcmx.command.WithdrawCommand;
import com.c2soft.nepalcmx.services.WithdrawlService;
import com.c2soft.nepalcmx.util.JsonUtil;
@Log4j2
@Controller
@RequestMapping("/bank")
public class WithdrawlController {
	Logger logger = LogManager.getLogger(WithdrawlController.class.getName());
	 
	@Autowired
	private WithdrawlService withdrawlService;
	
	
	@RequestMapping(value = "/withdrawl",method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String buy(@RequestBody WithdrawCommand withdraw) {
		
		logger.debug(withdraw);
		logger.debug(withdraw.getBrokeId());
		withdrawlService.withdrawlOrderService(withdraw);
		return "success";
	}

	@RequestMapping(value = "/withdraw",method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String withdrawl1(@RequestBody String json,HttpServletRequest req) {
		 WithdrawCommand withdraw= (WithdrawCommand) JsonUtil.fromJson(json,WithdrawCommand.class);
       String userId=(String) req.getSession().getAttribute("UsrId");
       	withdraw.setUser_Id(userId);
       	withdraw.setBrokeId("");
		withdrawlService.withdrawlOrderService(withdraw);
		return "{\"msg\":\"Withdrawal Request sent successfully.\"}";
	}
	
	@RequestMapping(value = "/bwithdrawl",method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String bwithdrawl(@RequestBody String json,HttpServletRequest req) {
		 WithdrawCommand withdraw= (WithdrawCommand) JsonUtil.fromJson(json,WithdrawCommand.class);
       String brokerId=(String) req.getSession().getAttribute("UsrId");
       	withdraw.setBrokeId(brokerId);
       	withdraw.setUser_Id("");
		withdrawlService.withdrawlOrderService(withdraw);
		return "{\"msg\":\"Withdrawal Request sent successfully.\"}";
	}
	
@RequestMapping(value = "/showWithdrawal",method=RequestMethod.GET, headers = "Accept=application/json")
	public @ResponseBody List<WithdrawCommand> getUserDetail(HttpServletRequest req) {
	String brokerId=(String) req.getSession().getAttribute("UsrId");
		List<WithdrawCommand> withdrawals=withdrawlService.getWithdrawals(brokerId);		
		return withdrawals;
	}


@RequestMapping(value = "/showBWithdrawal",method=RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<WithdrawCommand> getBrokerWithdrawal(HttpServletRequest req) {
	List<WithdrawCommand> withdrawals=withdrawlService.getBWithdrawals();		
	return withdrawals;
}

@RequestMapping(value = "/deleteWithdrawal", method={RequestMethod.GET,RequestMethod.POST})
@ResponseBody
public String deleteWithdrawal( HttpServletRequest req) {
	String id=req.getParameter("id");
	System.out.println("id:"+id);
	withdrawlService.deleterwithdrawalAccount(id);
	
	 return "{\"success\":\"success\"}";
}


}
